/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/08/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/  
package com.manager.workflow.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
import com.manager.workflow.entity.BusWorkItem;
import com.workflowdefinition.YfBussTaskinstance;
/**
 * 工作项数据库操作类
 * @author 王浩 QQ：873987060   
 *
 */
public class WorkItemDAO extends  HibernateGenericDao<BusWorkItem>{
	 private static final Logger logger = Logger.getLogger(WorkItemDAO.class);
	 
	 /**
	  * 获取代办工作项条数
	  * @param queryMap
	  * @return
	  */
	 public Integer getAllWorkItemCount(Map queryMap){
		    String actorId = (String)queryMap.get("actorId");
			List args = new ArrayList();
			StringBuffer sql = new StringBuffer();
			sql.append(" select count(0) from t_ff_rt_workitem wi left join t_ff_rt_taskinstance ta ");
			sql.append(" on wi.taskinstance_id = ta.id left join yf_buss_taskinstance yt ");
			sql.append(" on ta.id=yt.taskinstance_id where wi.actor_id=? and (wi.state='0' or wi.state='1') ");
            args.add(actorId);
            if(queryMap.get("type")!=null){
            	sql.append(" and yt.buss_type=?");
            	args.add(queryMap.get("type"));
            }
			return super.getCount(sql.toString(), args.toArray());
	}
	/**
	 * 获得工作项列表
	 * @param yfThesisForm
	 * @param pageBean
	 * @return
	 */
	public List getAllWorkItem(Map queryMap,final PageBean pageBean){  
		List args = new ArrayList();
		String actorId = (String)queryMap.get("actorId");
		StringBuffer sql = new StringBuffer();
		sql.append(" select wi.*,yt.*,ta.processinstance_Id from t_ff_rt_workitem wi left join t_ff_rt_taskinstance ta ");
		sql.append(" on wi.taskinstance_id = ta.id left join yf_buss_taskinstance yt ");
		sql.append(" on ta.id=yt.taskinstance_id where wi.actor_id=? and (wi.state='0' or wi.state='1')  ");
		args.add(actorId);
		 if(queryMap.get("type")!=null){
         	sql.append(" and yt.buss_type=?");
         	args.add(queryMap.get("type"));
         }
		 sql.append(" order by wi.created_time desc ");
		return getPageInfo(pageBean, sql.toString(), args);

	}
	
	 /**
	  * 获取代办工作项条数 总经理 角色  是看到最近完成的15条流程
	  * @param queryMap
	  * @return
	  */
	 public Integer getAllWorkItemCountZJL(Map queryMap){
		 try {
			 List args = new ArrayList();
				StringBuffer sql = new StringBuffer();
				sql.append(" 	 select count(distinct a.processinstance_Id) count from t_ff_rt_taskinstance a left join yf_buss_taskinstance b  ");
				sql.append("  on a.id = b.taskinstance_id  left join  t_ff_rt_processinstance c on a.processinstance_id = c.id  ");
				sql.append("  where b.buss_id is not null and c.state='7'  ");
		        if(queryMap.get("type")!=null){
		        	sql.append(" and b.buss_type=?");
		        	args.add(queryMap.get("type"));
		        }
				sql.append("  group by a.processinstance_Id  order by c.end_time limit 15 ");
				return super.getCount(sql.toString(),args.toArray());
		} catch (Exception e) {
			return 0;
		}
			
	}
	 
	    /**
		 * 获得工作项列表 总经理 角色  是看到最近完成的15条流程
		 * @param yfThesisForm
		 * @param pageBean
		 * @return
		 */
	public List getAllWorkItemZJL(Map queryMap,final PageBean pageBean){  
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" 	 select count(distinct a.processinstance_Id) count ,b.*,a.processinstance_Id  from t_ff_rt_taskinstance a left join yf_buss_taskinstance b  ");
		sql.append("  on a.id = b.taskinstance_id  left join  t_ff_rt_processinstance c on a.processinstance_id = c.id  ");
		sql.append("  where b.buss_id is not null and c.state='7'  ");
        if(queryMap.get("type")!=null){
        	sql.append(" and b.buss_type=?");
        	args.add(queryMap.get("type"));
        }
		sql.append("  group by a.processinstance_Id  order by c.end_time limit 15 ");

		return getPageInfo(pageBean, sql.toString(), args);

	}
	/**
	 * 指派任务预警信息
	 * @param queryMap
	 * @param pageBean
	 * @return
	 */
	public List getAllWarnTask(Map queryMap,final PageBean pageBean){  
		List args = new ArrayList();
		String actorId = (String)queryMap.get("actorId");
		StringBuffer sql = new StringBuffer();
		sql.append("select wi.*,yt.*,ta.processinstance_Id,s.complete_date,u.rname,f.tel,f.rname rname2 from t_ff_rt_workitem wi left join t_ff_rt_taskinstance ta  ");
		sql.append("  on wi.taskinstance_id = ta.id left join yf_buss_taskinstance yt  ");
		sql.append(" on ta.id=yt.taskinstance_id ");
		sql.append(" left join yf_appoint_task s on s.id = yt.buss_id  ");
		sql.append("  left join usertable u on s.operateid = u.id  ");
		sql.append("  left join usertable f on s.task_belong = f.id  where  (wi.state='0' or wi.state='1') ");
		 if(queryMap.get("actorId")!=null){
	         	sql.append(" and s.task_belong =? and wi.actor_id=?");
	         	args.add(queryMap.get("actorId"));
	         	args.add(queryMap.get("actorId"));
	         }
		 if(queryMap.get("type")!=null){
         	sql.append(" and yt.buss_type=?");
         	args.add(queryMap.get("type"));
         }
		 if(queryMap.get("diff")!=null){
	         	sql.append(" and timestampdiff(hour,now(),s.complete_date) < ?");
	         	args.add(queryMap.get("diff"));
	     }
		 sql.append(" order by wi.created_time desc ");
		return getPageInfo(pageBean, sql.toString(), args);

	}
	
	
	public Integer getAllWarnTaskCount(Map queryMap){  
		List args = new ArrayList();
		String actorId = (String)queryMap.get("actorId");
		StringBuffer sql = new StringBuffer();
		sql.append("select count(0) from t_ff_rt_workitem wi left join t_ff_rt_taskinstance ta  ");
		sql.append("  on wi.taskinstance_id = ta.id left join yf_buss_taskinstance yt  ");
		sql.append(" on ta.id=yt.taskinstance_id ");
		sql.append(" left join yf_appoint_task s on s.id = yt.buss_id  ");
		sql.append("  left join usertable u on s.operateid = u.id  where  (wi.state='0' or wi.state='1') ");
		 if(queryMap.get("actorId")!=null){
	         	sql.append(" and s.task_belong =?");
	         	args.add(queryMap.get("actorId"));
	         }
		 if(queryMap.get("type")!=null){
         	sql.append(" and yt.buss_type=?");
         	args.add(queryMap.get("type"));
         }
		 if(queryMap.get("diff")!=null){
	         	sql.append(" and timestampdiff(hour,now(),s.complete_date) < ?");
	         	args.add(queryMap.get("diff"));
	     }
		 return super.getCount(sql.toString(), args.toArray());

	}
	
	
	 /**
	  * 获取已办工作项条数
	  * @param queryMap
	  * @return
	  */
	 public Integer getAllHaveDoneWorkItemCount(Map queryMap){
		    String actorId = (String)queryMap.get("actorId");
			List args = new ArrayList();
			StringBuffer sql = new StringBuffer();
			sql.append(" select count(distinct ta.processinstance_Id) count from t_ff_rt_workitem wi left join t_ff_rt_taskinstance ta ");
			sql.append(" on wi.taskinstance_id = ta.id left join yf_buss_taskinstance yt ");
			sql.append(" on ta.id=yt.taskinstance_id where wi.actor_id=? and (wi.state='7' or wi.state='9') ");
            args.add(actorId);
            if(queryMap.get("type")!=null){
            	sql.append(" and yt.buss_type=?");
            	args.add(queryMap.get("type"));
            }
			return super.getCount(sql.toString(), args.toArray());
	}
	 
	
	
	 

	 
	/**
	 * 获取所有已办工作项列表
	 * @param queryMap
	 * @param pageBean
	 * @return
	 */
	public List getAllHaveDoneWorkItem(Map queryMap,final PageBean pageBean){  
		List args = new ArrayList();
		String actorId = (String)queryMap.get("actorId");
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(distinct ta.processinstance_Id) count ,wi.*,yt.*,ta.processinstance_Id from t_ff_rt_workitem wi left join t_ff_rt_taskinstance ta ");
		sql.append(" on wi.taskinstance_id = ta.id left join yf_buss_taskinstance yt ");
		sql.append(" on ta.id=yt.taskinstance_id where wi.actor_id=?  ");
		args.add(actorId);
		 if(queryMap.get("type")!=null){
	         	sql.append(" and yt.buss_type=?");
	         	args.add(queryMap.get("type"));
	         }
		 sql.append(" and (wi.state='7' or wi.state='9') group by ta.processinstance_Id order by wi.created_time desc ");
		return getPageInfo(pageBean, sql.toString(), args);

	}
	
	
	
 
	
	
	/**
	 * 获取流程实例编号
	 * @return
	 */
	public String getProcessInstanceId(String bussId,String bussType){
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from  t_ff_rt_taskinstance ta left join yf_buss_taskinstance yt");
		sql.append(" on ta.id=yt.taskinstance_id where yt.buss_id=? and yt.buss_type=? limit 1 ");
		args.add(bussId);
		args.add(bussType);
		BusWorkItem workItem = getPageInfo(null, sql.toString(), args).get(0);
		return workItem.getProcessinstanceId();
	}
	
	
	public YfBussTaskinstance getYfBussTaskinstance(String taskInstanceId){
		List args = new ArrayList();
		YfBussTaskinstance yfBussTaskinstance = new YfBussTaskinstance();
		StringBuffer sql = new StringBuffer();
		sql.append(" select * from  yf_buss_taskinstance ba left join t_ff_rt_taskinstance yt");
		sql.append(" on ba.TASKINSTANCE_ID=yt.id where yt.id=? order by created_time desc limit 1 ");
		args.add(taskInstanceId);
		try {
			List re = findBySQL(sql.toString(),args);
			for (Object object : re) {
				Map map = (Map) object;
				yfBussTaskinstance.setBussType(getLong(map.get("BUSS_TYPE")));
				yfBussTaskinstance.setBussId(getString(map.get("BUSS_ID")));
				yfBussTaskinstance.setProjectNo(getString(map.get("PROJECT_NO")));
				yfBussTaskinstance.setProjectName(getString(map.get("PROJECT_NAME")));
				yfBussTaskinstance.setProjectOrg(getString(map.get("PROJECT_ORG")));
			}
		} catch (Exception e) {
	
			e.printStackTrace();
		}
		return yfBussTaskinstance;
	}
	
	private Long getLong(Object o){
		if(o!=null){
			return Long.valueOf(o.toString());
		}
		return 0l;
	}
	
	private String getString(Object o){
		if(o!=null){
			return (String) o;
		}
		return "";
	}
	        
}
